package com.ky.facescan.device.telpo;


import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import android.util.Log;

import com.ky.facescan.utils.ToastUtils;
import com.telpo.tps550.api.idcard.IdCard;
import com.telpo.tps550.api.idcard.IdentityMsg;
import com.telpo.tps550.api.idcard.T2OReader;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class IDCardScan {

    Handler mHandler = new Handler(Looper.myLooper());

    private static volatile IDCardScan singleton;
    private volatile boolean mLooping = false;
    private boolean mFoundIdCard = false;
    private T2OReader mT2OReader;
    private IDCardScanCallback idCardScanCallback;

    public static IDCardScan getInstance(Context context) {
        if (singleton == null) {
            synchronized (IDCardScan.class) {
                if (singleton == null) {
                    singleton = new IDCardScan(context);
                }
            }
        }
        return singleton;
    }

    public IDCardScan(Context context) {
        mT2OReader = new T2OReader();
//        mHandler.post(() -> ToastUtils.showSnackBar("读卡模块在初始化中请稍候..."));
        if (!mT2OReader.isUSBReader(context)) {
            mHandler.post(() -> ToastUtils.show("设备没有安装身份证读卡模块"));
            return;
        }
        if (mT2OReader.openReader(context)) {
            Log.e("Hello", "run == > 读卡器初始化成功");
            mHandler.post(() -> ToastUtils.show("读卡器初始化成功"));

        } else {
            Log.e("Hello", "run == > 读卡器初始化失败");
            mHandler.post(() -> ToastUtils.show("读卡器初始化失败"));
        }
        startIdCardScan();
    }

    /**
     * 读取身份证
     */
    private void startIdCardScan() {
        mLooping = true;
        ExecutorService ste = Executors.newSingleThreadExecutor();
        ste.execute(() -> {
            while (mLooping) {
                //读取身份证
                if (mT2OReader.findIDCard()) {
                    if (mFoundIdCard) {
                        SystemClock.sleep(50);
                        continue;
                    }
                    if (mT2OReader.selectIDCard()) {
                        IdentityMsg identityMsg = IdCard.decodeIdCardBaseInfo(mT2OReader.readIDCard(false));
                        if (identityMsg != null && idCardScanCallback != null) {
                            mHandler.post(() -> {
                                //解密身份头像图片
                                // Bitmap bitmap = mT2OReader.decodeIDImage(identityMsg.getHead_photo());
                                String name = identityMsg.getName().replace(" ", "");
                                identityMsg.setName(name);
                                if (idCardScanCallback != null) {
                                    idCardScanCallback.idCardResult(identityMsg);
                                }
                            });
                            mFoundIdCard = true;
                        }
                    }
                } else {
                    mFoundIdCard = false;
                }
                SystemClock.sleep(5);
            }
        });
    }

    public void setIdCardScanCallback(IDCardScanCallback idCardScanCallback) {
        this.idCardScanCallback = idCardScanCallback;
    }

    public void stop() {
        mLooping = false;
    }

    public void resume() {
        mLooping = true;
    }

    public void destroy() {
        mLooping = false;
        if (mT2OReader != null) {
            mT2OReader.closeReader();
        }
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
        singleton = null;
    }

    public interface IDCardScanCallback {
        void idCardResult(IdentityMsg identityInfo);
    }
}
